package com.easybenefit.UUService.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easybeanfit.UUService.commutill.UUDialogUtill;
import com.easybeanfit.UUService.commutill.UUJsonUtil;
import com.easybeanfit.UUService.commutill.UULoadPictureUtill;
import com.easybeanfit.UUService.commutill.UUNetUtill;
import com.easybeanfit.UUService.commutill.UUSharedUtil;
import com.easybeanfit.UUService.commutill.UUToastUtil;
import com.easybeanfit.UUService.config.EnumShopNextStatus;
import com.easybeanfit.UUService.config.EnumShopStatus;
import com.easybeanfit.UUService.config.UUConfig;
import com.easybeanfit.UUService.config.UUDict;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.activity.CouponsDetailActivity;
import com.easybenefit.UUService.model.CouponlistBean;
import com.easybenefit.UUService.model.HttpCouponBean;
import com.easybenefit.UUService.model.MerchantBean;
import com.easybenefit.UUService.model.UserInfo;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.List;

/**
 * Created by 46595 on 2016/6/9.
 */
public class MerchantsListAdapter extends BaseAdapter {
    private List<MerchantBean> list;
    private Context context;
    private int type;
    private String url="";
    private RequestParams params=new RequestParams();
    private String code_name__1 = "重新申请联盟";
    private String code_name__2 = "重新申请联盟";
    private String code_name_0 = "申请联盟";
    private String code_name_1 = "取消联盟";
    private String code_name_2 = "取消申请联盟";
    private String code_name_3 = "同意联盟/拒绝联盟";
    /**
     * @param list
     * @param context
     */
    public MerchantsListAdapter(List<MerchantBean> list, Context context) {
        this.list = list;
        this.context = context;

    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_merchants, null);
            viewHolder = new ViewHolder();
            viewHolder.tv__name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_status = (TextView) convertView.findViewById(R.id.tv_status);
            viewHolder.tv_shop_status = (TextView) convertView.findViewById(R.id.tv_shop_status);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image_item);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv__name.setText(list.get(position).getMer_name());
        MerchantBean cop = list.get(position);
        viewHolder.tv_shop_status.setText(cop.getStatus());
        switch (cop.getStatus()) {
            case "-1":
                viewHolder.tv_status.setTextColor(Color.RED);
                viewHolder.tv_status.setText(code_name__1);
                break;
            case "-2":
                viewHolder.tv_status.setTextColor(Color.RED);
                viewHolder.tv_status.setText(code_name__2);
                break;
            case "0":
                viewHolder.tv_status.setTextColor(Color.RED);
                viewHolder.tv_status.setText(code_name_0);
                break;
            case "1":
                viewHolder.tv_status.setTextColor(Color.RED);
                viewHolder.tv_status.setText(code_name_1);
                break;
            case "2":
                viewHolder.tv_status.setTextColor(Color.rgb(85,85,85));
                viewHolder.tv_status.setText(code_name_2);
                break;
            case "3":
                viewHolder.tv_status.setTextColor(Color.RED);
                viewHolder.tv_status.setText(code_name_3);
                break;
        }
        viewHolder.tv_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MerchantBean cop = list.get(position);
                String mer_id= ((UserInfo)UUSharedUtil.getObject(context,UUConfig.UU_USERINFO_NAME, UserInfo.class)).getMer_id();
                requestUnion(cop.getMer_id(),mer_id, viewHolder.tv_shop_status.getText().toString(), viewHolder.tv_status,viewHolder.tv_shop_status);
            }
        });
        UULoadPictureUtill loadPicture = new UULoadPictureUtill(context, viewHolder.imageView);
        loadPicture.getmImageLoader().get(UUConfig.BASE_URL + list.get(position).getMer_logo(), loadPicture.getOne_listener());

        return convertView;
    }

    class ViewHolder {

        public TextView tv__name, tv_status,tv_shop_status;
        public ImageView imageView;

    }

    /**
     * 申请联盟
     *
     * @param coop_id 对方商户id
     * @param status  当前商户申请状态
     * @param status  商户状态控件
     * @param mer_id  商户状态控件
     */
    public void requestUnion(String coop_id, String mer_id, final String status, final TextView statusText,final TextView tv_shop_status) {
        switch (status){
                case "-2":
                    //重新申请联盟
                    url=UUConfig.SERVER_BASE_URL+"againmercoop";
                    params.put("coop_id",coop_id);
                    params.put("mer_id",mer_id);
                    updateStatus(url,params,statusText,tv_shop_status,status);
                    break;
                case "-1":
                    //重新申请联盟
                    url=UUConfig.SERVER_BASE_URL+"againmercoop";
                    params.put("coop_id",coop_id);
                    params.put("mer_id",mer_id);
                    updateStatus(url,params,statusText,tv_shop_status,status);
                    break;
                case "0":
                    //申请联盟
                    url=UUConfig.SERVER_BASE_URL+"addmercoop";
                    params.put("coop_id",coop_id);
                    params.put("mer_id",mer_id);
                    updateStatus(url,params,statusText,tv_shop_status,status);
                    break;
                case "1":
                    //取消联盟
                    url=UUConfig.SERVER_BASE_URL+"quitmercoop";
                    params.put("coop_id",coop_id);
                    params.put("mer_id",mer_id);
                    updateStatus(url,params,statusText,tv_shop_status,status);
                    break;
                case "2":
                    //取消申请联盟
                    url=UUConfig.SERVER_BASE_URL+"backmercoop";
                    params.put("coop_id",coop_id);
                    params.put("mer_id",mer_id);
                    updateStatus(url,params,statusText,tv_shop_status,status);
                    break;
                case "3":
                    //同意联盟/不同意联盟
                    params.put("coop_id",coop_id);
                    params.put("mer_id",mer_id);
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setMessage("请选择是否同意联盟");
                    builder.setTitle("提示");
                    builder.setPositiveButton("同意联盟", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            url=UUConfig.SERVER_BASE_URL+"agreemercoop";
                            updateStatus(url,params,statusText,tv_shop_status,"3");
                        }
                    });
                    builder.setNegativeButton("拒绝联盟", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            url=UUConfig.SERVER_BASE_URL+"refmercoop";
                            updateStatus(url,params,statusText,tv_shop_status,"4");
                        }
                    });
                    builder.show();
                    break;
            }
    }

    /**
     * 申请联盟网络方法
     *
     * @param url
     * @param params
     * @param statusText
     */
    public void updateStatus(String url, RequestParams params, final TextView statusText,final TextView tv_shop_status, final String status) {
        if (!UUNetUtill.isConnected(context)) {
            UUToastUtil.showToast(context, UUDict.NOT_INTERNET);
        }else{
            AsyncHttpClient client = new AsyncHttpClient();
            client.post(url, params, new AsyncHttpResponseHandler() {

                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    String content = new String(bytes);
                    HttpCouponBean http = (HttpCouponBean) UUJsonUtil.fromJson(content, HttpCouponBean.class);
                    if("200".equals(http.getCode())){
                        UUToastUtil.showToast(context, http.getMessage());
                        switch (status) {
                            case "-1"://如果当前状态为重新申请联盟，点击后变为取消申请联盟
                                tv_shop_status.setText("2");
                                statusText.setTextColor(Color.rgb(85,85,85));
                                statusText.setText(code_name_2);
                                break;
                            case "-2"://如果当前状态为重新申请联盟，点击后变为取消申请联盟
                                tv_shop_status.setText("2");
                                statusText.setTextColor(Color.rgb(85,85,85));
                                statusText.setText(code_name_2);
                                break;
                            case "0"://如果当前状态为申请联盟 0，点击后变为取消申请联盟2
                                tv_shop_status.setText("2");
                                statusText.setTextColor(Color.rgb(85,85,85));
                                statusText.setText(code_name_2);
                                break;
                            case "1"://如果当前状态为取消联盟 1，点击后变为重新申请联盟 -1
                                tv_shop_status.setText("-1");
                                statusText.setTextColor(Color.RED);
                                statusText.setText(code_name__1);
                                break;
                            case "2"://如果当前状态为取消申请联盟 2，点击后变为申请联盟 0
                                tv_shop_status.setText("0");
                                statusText.setTextColor(Color.RED);
                                statusText.setText(code_name_0);
                                break;
                            case "3"://如果当前状态为对方申请联盟 3，如果点击同意联盟变为取消联盟 1
                                tv_shop_status.setText("1");
                                statusText.setTextColor(Color.rgb(85,85,85));
                                statusText.setText(code_name_1);
                                break;
                            case "4"://如果当前状态为对方申请联盟 3，如果点击拒绝联盟变为申请联盟 0
                                tv_shop_status.setText("0");
                                statusText.setTextColor(Color.RED);
                                statusText.setText(code_name_0);
                                break;
                        }
                    }else{
                        UUToastUtil.showToast(context, http.getMessage());
                    }
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }
            });
        }
    }
}
